package com.rpc.client;

import com.rpc.codec.RpcDcoder;
import com.rpc.codec.RpcEncoder;
import com.rpc.codec.RpcRequest;
import com.rpc.codec.RpcResponse;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * @Description  client编解码器
 * @Author 顾志杰
 * @Date 2022/2/28 11:19
 */
public class RpcClientInitializer extends ChannelInitializer<SocketChannel> {
  @Override
  protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline cp = ch.pipeline();
    /*编解码的handler*/
    cp.addLast(new RpcEncoder(RpcRequest.class));
    cp.addLast(new LengthFieldBasedFrameDecoder(
      /*整个数据包大小*/
      65536,
      /*数据包起始位置*/
      0,
      /*第一个关键数据位置(编码的时候包头有4个字节从第5个开始是真正的数据)*/
      4,
      0,
      0
      ));
    cp.addLast(new RpcDcoder(RpcResponse.class));
    /*实际的业务处理器RpcClientHandler*/
    cp.addLast(new RpcClientHandler());

  }
}
